package com.justnow.solution;


/**
 * 特殊案列：
 * +0
 * -0
 * +-2
 * -+2
 * ""
 * " "
 * -abc
 * 12345 abc
 * "1"
 */
public class Solution01 {


    /**
     *
     * @param str
     * @return
     */
    public int myAtoi(String str) {
        if (str == null || str.replace(" ", "").equals("") || str.equals("+") || str.equals("-") )
            return 0;
        if ((str.length() >= 2) && (str.substring(0, 2).equals("+-")  || str.substring(0, 2).equals("-+") ))
            return 0;
        int res = 0;
        //获取第一个非空字符
        int start = 0;
        while (str.charAt(start) == ' ') {
            start++;
        }
        char first = str.charAt(start);
        int end = start + 1;
        if (first == '+' || first == '-' || (first-'0'>= 0 && first-'0' <= 9)) {
            if (end == str.length()) {
                return first - '0';
            }
            int endNum = str.charAt(end) - '0';
            while ((end <= str.length() - 1) && ( endNum >=0 && endNum <= 9)) {
                if (end + 1 <= str.length() - 1) {
                    int next = str.charAt(end+1) - '0';
                    if (next < 0 || next > 9) {
                        end++;
                        break;
                    }

                }
                end++;
            }

            try{
                Integer integer = Integer.valueOf(str.substring(start, end));
                res = integer;
            }catch (Exception e) {
                if (first == '-') {
                    res = Integer.MIN_VALUE;
                } else {
                    res = Integer.MAX_VALUE;
                }
            }
        }

        return res;
    }

    public static void main(String[] args) {
        Solution01 solution01 = new Solution01();
/*        int test1 = solution01.myAtoi("-91283472332");
        System.out.println(test1);*/
        int test2 = solution01.myAtoi("   -42");
        System.out.println(test2);
        Integer.parseInt("+-100");
        Integer.valueOf("-100");
    }
}
